import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/user/Users.vue'
import Rights from '../views/power/Rights.vue'
import Roles from '../views/power/Roles.vue'
import Categories from '../views/goods/Categories.vue'
import Params from '../views/goods/Params.vue'
import GoodsList from '../views/goods/List.vue'
import Add from '../views/goods/Add.vue'
import Order from '../views/order/Order.vue'
import Report from '../views/report/Report.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Categories },
      { path: '/params', component: Params },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report },
    ]
  }
]

const router = new VueRouter({
  routes
})
//路由守卫
router.beforeEach((to, from, next) => {
  //如果是登录页,就不需要拦截
  if (to.path === '/login') {
    return next()
  }
  //不是登录页就需要判断是否有token,没有就没有登录
  const tokenStr = window.sessionStorage.getItem('token')
  //没有token
  if (!tokenStr) {
    return next('/login')
  }
  //有token, 进行下一步
  next()
})

export default router
